package com.android.questions.ui.adapter;

import android.annotation.SuppressLint;
import android.text.Html;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.android.base.util.ActivityClickUtil;
import com.android.questions.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hx.currency.data.qa.model.QAInvite;

/**
 * FileName: InviteAdapter
 * Author: admin
 * Date: 2021/11/4 15:07
 * Description:
 */
public class InviteAdapter extends BaseQuickAdapter<QAInvite, BaseViewHolder> {
    private OnItemChildClickLister clickLister;
    private int inviteCount = 0;

    public InviteAdapter() {
        super(R.layout.item_invite);
    }

    @Override
    protected int getDefItemViewType(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(final BaseViewHolder baseViewHolder, QAInvite item) {
        AppCompatTextView tvTitle = baseViewHolder.getView(R.id.item_title);
        AppCompatTextView tvCash = baseViewHolder.getView(R.id.item_cash);
        AppCompatTextView tvProgress = baseViewHolder.getView(R.id.item_tv_progress);
        AppCompatImageView ivReceive = baseViewHolder.getView(R.id.item_receive);

        String title = "成功邀请<b><font color='#FD0D21'>" + item.getIn() + "</font></b>位有效用户";
        tvTitle.setText(Html.fromHtml(title));

        String cash = "可领<b>" + item.getWa() + "</b>元";
        tvCash.setText(Html.fromHtml(cash));

        final int state = item.getGs();
        if (state == 0) {
            ivReceive.setEnabled(true);
            ivReceive.setImageResource(R.drawable.icon_invite_go_complete);
        } else if (state == 1) {
            ivReceive.setEnabled(true);
            ivReceive.setImageResource(R.drawable.icon_invite_withdrawal);
        } else {
            ivReceive.setEnabled(false);
            ivReceive.setImageResource(R.drawable.icon_invite_withdrawal_complete);
        }

        int curInviteCount = inviteCount;
        if (curInviteCount >= item.getIn()) {
            curInviteCount = item.getIn();
        }
        String strProgress = "任务进度：<font color='#FD0D21'>" + curInviteCount + "</font>/" + item.getIn();
        tvProgress.setText(Html.fromHtml(strProgress));

        ivReceive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityClickUtil.getInstance().isFastClick()) {
                    //防止用户快速点击
                    return;
                }
                if (state == 0) {
                    if (clickLister != null) {
                        clickLister.onInvite();
                    }
                } else {
                    if (clickLister != null) {
                        int position = getItemPosition(item);
                        clickLister.onWithdrawal(item, position);
                    }
                }
            }
        });
    }

    public OnItemChildClickLister getClickLister() {
        return clickLister;
    }

    public void setInviteCount(int inviteCount) {
        this.inviteCount = inviteCount;
    }

    public void setClickLister(OnItemChildClickLister clickLister) {
        this.clickLister = clickLister;
    }

    public interface OnItemChildClickLister {
        void onWithdrawal(QAInvite item, int pos);

        void onInvite();
    }
}